/*! Copyright(c) 2008-2012
 *
 *\file		spi_flash.h
 *\brief		
 *\details	
 *
 *\author	Cai Peifeng
 *\version	
 *\date		30Jun12
 *
 *\warning	
 *
 *\history \arg	30Jun12, Cai Peifeng, Create the file
 */
#ifndef __SPI_FLASH_H__
#define __SPI_FLASH_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "Stm8s.h"

#define SPI_CS				GPIOA
#define SPI_CS_PIN			GPIO_PIN_2
#define SPI_FLASH_PageSize	0x100

#define	WRITE		0x02	/*!<  (add by Cai Peifeng:2012-6-30 22:20:56)*/
#define	WRSR		0x01	/*!<  (add by Cai Peifeng:2012-6-30 22:21:15)*/
#define WREN		0x06	/*!<  (add by Cai Peifeng:2012-6-30 22:21:26)*/
#define READ		0x03	/*!<  (add by Cai Peifeng:2012-6-30 22:21:36)*/
#define RDSR		0x05	/*!<  (add by Cai Peifeng:2012-6-30 22:21:59)*/
#define RDID		0x9F	/*!<  (add by Cai Peifeng:2012-6-30 22:22:12)*/
#define SE			0xD8	/*!<  (add by Cai Peifeng:2012-6-30 22:22:26)*/
#define BE			0xC7	/*!<  (add by Cai Peifeng:2012-6-30 22:22:40)*/

#define WIP_Flag	0x01	/*!<  (add by Cai Peifeng:2012-6-30 22:23:5)*/
#define Dummy_Byte	0xA5	/*!<  (add by Cai Peifeng:2012-6-30 22:23:37)*/

#define	FLASH_WriteAddress	0x00000000
#define FLASH_ReadAddress	FLASH_WriteAddress
#define	FLASH_SectorToErase	FLASH_WriteAddress

#define SPI_FLASH_CS_LOW()		GPIO_WriteLow(SPI_CS, SPI_CS_PIN)
#define SPI_FLASH_CS_HIGH()		GPIO_WriteHigh(SPI_CS, SPI_CS_PIN)

/*!
 *\fn			void SPI_FLASH_Init();
 *\brief   		init the spi flash
 *
 *\param[in]   
 *\param[out]  
 *
 *\return  
 *\retval  
 *
 *\note		  
 */
void SPI_FLASH_Init();


/*!
 *\fn			uint8_t SPI_FLASH_SendByte(uint8_t byte);
 *\brief   		send a byte through the spi interface and return the byte received from the spi bus
 *
 *\param[in]   
 *\param[out]  
 *
 *\return  
 *\retval  
 *
 *\note		  
 */
uint8_t SPI_FLASH_SendByte(uint8_t byte);

/*!
 *\fn			void SPI_FLASH_WriteEnable();
 *\brief   		enable the write access to the flash
 *
 *\param[in]   
 *\param[out]  
 *
 *\return  
 *\retval  
 *
 *\note		  
 */
void SPI_FLASH_WriteEnable();

/*!
 *\fn			void SPI_FLASH_WaitForWriteEnd();
 *\brief   		polls the status of the write in progress flah in the flash's status register and 
 				loop until write operation
 *
 *\param[in]   
 *\param[out]  
 *
 *\return  
 *\retval  
 *
 *\note		  
 */
void SPI_FLASH_WaitForWriteEnd();

/*!
 *\fn			void SPI_FLASH_PageWrite(uint8_t pBuffer, uint32_t WriteAddr, uint16_t WriteNum);
 *\brief   		write more than one byte to the flash wite a single write cycle. the number can't
 				exceed the flash page size
 *
 *\param[in]   
 *\param[out]  
 *
 *\return  
 *\retval  
 *
 *\note		  
 */
void SPI_FLASH_PageWrite(uint8_t *pBuffer, uint32_t WriteAddr, uint16_t WriteNum);

/*!
 *\fn			void SPI_FLASH_BufferRead(uint8_t pBuffer, uint32_t ReadAddr, uint16_t ReadNum);
 *\brief   		 Reads a block of data from the FLASH.
 *
 *\param[in]   
 *\param[out]  
 *
 *\return  
 *\retval  
 *
 *\note		  
 */
void SPI_FLASH_BufferRead(uint8_t *pBuffer, uint32_t ReadAddr, uint16_t ReadNum);

/*!
 *\fn			void SPI_FLASH_BufferWrite(uint8_t pBuffer, uint32_t WriteAddr, uint16_t WriteNum);
 *\brief   		Writes block of data to the FLASH
 *
 *\param[in]   
 *\param[out]  
 *
 *\return  
 *\retval  
 *
 *\note		  
 */
void SPI_FLASH_BufferWrite(uint8_t *pBuffer, uint32_t WriteAddr, uint16_t WriteNum);

/*!
 *\fn			uint32_t SPI_FLASH_ReadID();
 *\brief   		read the flash id
 *
 *\param[in]   
 *\param[out]  
 *
 *\return  
 *\retval  
 *
 *\note		  
 */
uint32_t SPI_FLASH_ReadID();

/*!
 *\fn			void SPI_FLASH_SectorErase(uint32_t SectorAddr)
 *\brief   		block erase
 *
 *\param[in]   
 *\param[out]  
 *
 *\return  
 *\retval  
 *
 *\note		  
 */
void SPI_FLASH_SectorErase(uint32_t SectorAddr);


#ifdef __cplusplus
}
#endif

#endif	/* __SPI_FLASH_H__ */
